

$(function () {

    var filebutton = $('<div id="upfilesButton" class="btn btn-block btn-info btn-sm"><i class="icon-upload icon-white"></i>上传配图</div>');
    var fileImage = $('<div id="upfilesButtonImages"></div>');





    //$("#upfilediv").append(fileObj);
    $("#upfilediv").append(filebutton);
    $("#upfilediv").append(fileImage);



    if (theLoadImage.length > 1) {
        theLoadImage = theLoadImage;
        editImage(theLoadImage)
    }




    var button = $('#upfilesButton'), interval;


    new AjaxUpload(button, {
        action: '../uploadimage',
        name: 'plug_files',
        data: { action: ActionType_push, uploadPath: ActionType_uploadPath,csrf_token:GetXSRF()},
        onSubmit: function (file, ext) {
            button.text('上传中...');

            this.disable();

            // Uploding -> Uploading. -> Uploading...
            interval = window.setInterval(function () {
                var text = button.text();
                if (text.length < 13) {
                    button.text(text + '.');
                } else {
                    button.text('...');
                }
            }, 200);
        },
        onComplete: function (file, response) {
            button.text('上传配图');

            //response = response.Replce()

            //alert(response);

            window.clearInterval(interval);

            // enable upload button
            this.enable();

            var obj = eval('(' + response + ')');

            if (obj.relues == "1") {



                editImage(obj.pic);



            }
            else {
                alert(obj.err);
            }



        }

    });


    function editImage(src) {
        var theImagefile = $("#theImagefile");
        var imgsrc = src;
        var saveserverpath = theImagefile.attr('saveserverpath');
        if (saveserverpath.length > 0) imgsrc = saveserverpath + "/" + imgsrc;



        var theImage = $('<img alt="" src="' + imgsrc + '" style="border:1px solid #ccc ; padding:5px ; width:250px" />');
        var theRelse = $('<div class="btn btn-mini btn-danger"><i class="icon-remove icon-white"></i>删除照片</div>');



        theImagefile.val(src);

        $(theRelse).click(function () {



            $.post('customform/upimages.ashx', { action: ActionType_del, tsfile: src }, function (result) {


                var obj = eval('(' + result + ')');

                if (obj.relues == "1") {
                    filebutton.show();
                    fileImage.html('');
                    theRelse.remove();
                    $("#theImagefile").val(src);
                    $("#theImagefile").val('');
                    return;
                }

            });


        });



        fileImage.append(theImage);
        filebutton.hide();
        $("#upfilediv").append(theRelse);


    }

});